<template>
  <div class="app-container">
    <el-card class="box-card" shadow="never" style="border:0px;">
      <div slot="header" style="color:#303133;font-size:14px;font-weight:bold;">
        <span>渠道管理</span>
      </div>
    <div class="filter-container">
      <div class="left">
        <el-form :inline="true" class="demo-form-inline" size="medium" label-position="right">
          <el-form-item label="渠道名称：">
            <el-input v-model="listQuery.channelName" clearable class="filter-item" placeholder="请输入渠道名称" />
          </el-form-item>
          <el-form-item label="法人姓名：">
            <el-input v-model="listQuery.name" clearable class="filter-item" placeholder="请输入法人姓名" />
          </el-form-item>
         
        </el-form>
      </div>
      <div class="right">
        <el-button type="primary"   @click="getList()">查询</el-button>
        <el-button  @click="restForm">重置</el-button>
      </div>
    </div>

    <vxe-toolbar ref="xToolbar">
         <template #buttons>
         <el-button v-permission="'system:channel-list-menu:add'"  type="text" icon="el-icon-plus" @click="uploadRepayPlan">新增渠道</el-button>
        </template>
    </vxe-toolbar>
    <vxe-table
      v-loading="loading" 
      :data="list"
      border="none"
      highlight-hover-row
      show-header-overflow
      show-overflow
      align="center"
      :header-cell-style="{ background: '#F7F7F8', color: '#606266' }"
      @cell-dblclick="cellDblclick"
    >
      <vxe-table-column type="seq" title="序号" min-width="60"/>
      <vxe-table-column title="渠道名称" field="channelName" min-width="300" >
         <template slot-scope="scope">
           <el-link type="primary" @click="toDetail(scope.row)">{{scope.row.channelName}}</el-link>
        </template>
      </vxe-table-column>
      <vxe-table-column title="渠道简称" field="alias" min-width="200"/>
      <vxe-table-column title="合作状态" field="cooperationStatusName" min-width="200"/>
      <vxe-table-column title="法人姓名" field="legalPersonName" min-width="200"/>
      <vxe-table-column title="状态" field="status" min-width="120">
        <template slot-scope="scope">
           <span>
              <span>
                <i
                  class="smallCircle"
                  :class="[
                    scope.row.status === 1 ? 'text_active' : 'text_noActive'
                  ]"
                />{{ scope.row.status === 1 ? '启用' : '禁用' }}
              </span>
            </span>
        </template>
      </vxe-table-column>
      <vxe-table-column title="创建时间" field="createTime" min-width="200"/>
      <vxe-table-column title="操作" fixed="right" min-width="300">
        <template v-slot="{ row }">
           <el-button v-permission="'system:channel-list-menu:detail'"   type="text" size="medium" @click="toDetail(row)">查看</el-button>
           <el-divider direction="vertical"></el-divider>
          <el-button  v-permission="'system:channel-list-menu:edit'"   type="text" size="medium" @click="toEdit(row)">编辑</el-button>
          <el-divider direction="vertical"></el-divider>
          <!-- <el-button v-permission="'system:channel-list-menu:edit'"  type="danger" size="small" round @click="deleteData(row)">删除</el-button> -->
          <el-button  type="text" size="medium" @click="enableStatus(row)" v-if="row.status === 0">启用</el-button>
          <el-button  type="text" size="medium" @click="disableStatus(row)" v-if="row.status === 1">禁用</el-button>
         
        </template>
      </vxe-table-column>
    </vxe-table>
    </el-card>
    <vxe-pager
      class="pager"
      @page-change="changePage"
      :current-page.sync="listQuery.pageNo"
      :page-size.sync="listQuery.pageSize"
      :total="total"
      :page-sizes="[5, 10, 30, 50, 100]"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
    >
    </vxe-pager>
  </div>
</template>

<script>
import { listChannel,deleteChannel,createOrUpdateChannel } from '@/api/channel';
import DicSelect from '@/components/select/index';
export default {
  name: 'User',
  components: { DicSelect },
  data: function () {
    return {
      active: 0,
      list: [],
      total: 0,
      listQuery: {
        pageNo: 1,
        pageSize: 10,
        channelName: undefined,
        name: undefined,
      },
      loading: true
    };
  },
  watch: {
    "$route":((to,form)=>{
      console.log(to)
    })
  },
  mounted: function () {
    this.getList();
  },
  created(){
    this.getList();
  },
  // getTask
  methods: {
    // 列表查询
    getList() {
      this.loading = true;
      listChannel(this.listQuery).then((data) => {
        this.list = data.data;
        this.total = data.total;
        console.log('响应数据:%o', data);
      });
      this.loading = false;
    },
    toEdit(row) {
      this.$router.push({
        path: '/channel/add',
        query: { channelId: row.id},
      });
    },
    toDetail(row) {
      this.$router.push({
        path: '/channel/add',
        query: { channelId: row.id,detail: true},
      });
    },
    lookProjectDetail(row) {
      //跳转到审批页面
      this.$router.push({ path: '/task/deal' , query: { projectId: row.projectId }})
    },
    changePage() {
      this.getList();
    },
    uploadRepayPlan(){
        this.$router.push("/channel/add")
    },
    restForm() {
      this.listQuery = {
        pageNo: 1,
        pageSize: 10,
        channelName: undefined,
        name: undefined,
      };
      this.getList();
    },
    enableStatus(row) {
      row.status = 1;
      this.updateById(row);
    },
    disableStatus(row) {
      row.status = 0;
      this.updateById(row);
    },
   updateById(row){
          let showMessageContent = ""
          if(row.status === 1) {
             showMessageContent = "启用成功"
          }else{
            showMessageContent = "禁用成功"
          }
          createOrUpdateChannel(row)
            .then(response => {
                this.$notify.success({
                title: '成功',
                message: showMessageContent
              })
              this.getList()
            
            })
            .catch(response => {
            })
   },
    deleteData(row){
       this.$confirm('确定要删除此渠道吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
           deleteChannel({channelId:row.id})
            .then((data) => {
              this.$notify.success("删除成功")
              this.getList()
            })
            .catch((err) => {
              console.error('删除失败');
            });
        }).catch(() => {
           this.$message("已取消删除")
        });
    },
    cellDblclick({row,rowIndex,column,columnIndex,cell},event) {
      let _this = this;
      this.$copyText(event.target.innerText).then(function() {
        _this.$message.info('复制成功');
      });
    }

  },
};
</script>

<style lang="css" scoped>
.filter-container {
  display: flex;
  justify-content: space-between;
  /* margin-top: 60px; */
}

.pager{
  position: fixed;
  bottom: 0;
  right: 28px;
  background-color: #FAFAFB;
  z-index: 2;
  height: 40px;
  width: calc(100% - 260px);
  }

.right >>> .el-button--primary,.el-button--default {
  padding: 10px 20px;
}

.vxe-table--render-default >>> .el-button--text{
  color: #2b7bfb;
}
.app-container >>> .vxe-table--render-default,.vxe-pager {
  font-family: PingFangSC, Arial, Helvetica, sans-serif;
} 

.left {
  flex: 14;
  display: flex;
  flex-flow: row wrap;
  margin-right: 20px;
}
.left >>> .el-form {
  width: 100%;
}
.left >>> .el-form .el-form-item {
  display: flex;
  flex-flow: row nowrap;
  width: 30%;
  margin-right: 3%;
  float: left;
  margin-bottom: 14px;
}

.left >>> .el-form .el-form-item .el-form-item__label {
  font-weight: 500;
  color: #606266;
  text-align: left;
  /* flex: 1; */
}

.left >>> .el-form .el-form-item .el-form-item__content {
  flex: 1;
  /*超出部分隐藏*/
  text-overflow: ellipsis;
  /* 超出部分显示省略号 */
  white-space: nowrap;
  /*规定段落中的文本不进行换行 */
  width: 100%;
}
.date_item {
  width: 100%;
}
.right {
  flex: 2;
  display: flex;
  flex-flow: row nowrap;
  align-items: flex-end;
  margin-right: 30px;
  /* margin-bottom: 30px; */
}
</style>
